package com.example.demo.controller;


import com.example.demo.api.SmsApi;
import com.example.demo.dao.monDao.PersonResultDao;
import com.example.demo.dto.MingPian;
import com.example.demo.dto.Persion_ID;
import com.example.demo.dto.YingYe;
import com.example.demo.dto.monDTO.PersonResultMongo;
import com.example.demo.service.Tencent_OCRService;
import com.example.demo.utils.FileSaveLocalUtil;
import com.example.demo.utils.KeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@Api(tags = "OCR识别")
public class OCRController {
    @Autowired
    private Tencent_OCRService tencent_ocrService;
    @Autowired
    private PersonResultDao personResultDao;  //直接注入不写service了。
    @PostMapping("/getOCR_")
    @ApiOperation("基本识别")
    public String getOcr_(@ApiParam("提交需要识别的照片") @RequestParam("file")MultipartFile file,
                          @ApiParam("ocr的类型参数:<br>(乱传数字默认为通用识别)<br>0  ->  身份证识别<br>1  ->  名片查询<br>2  ->  营业执照<br>3  ->  银行卡<br>4  ->  通用")
                          @RequestParam("ocr")String ocr,
                          @ApiParam("用户openid,可提交可不提交，不提交则不会保存") @RequestParam(value = "openid",defaultValue = "undefined") String openid)throws Exception {
        String fileName = KeyUtil.genUniqueKey();
        FileSaveLocalUtil.savePic(file.getInputStream(), fileName);
        String result = tencent_ocrService.getOCR_(file.getBytes(),ocr);
        if("0".equals(ocr)){  //身份证
            JSONParser parser=new JSONParser();
            JSONObject data = (JSONObject) parser.parse(result);
            Persion_ID persion_id = new Persion_ID(data);
            PersonResultMongo personResultMongo = new PersonResultMongo();
            personResultMongo.setId(KeyUtil.genUniqueKey());
            personResultMongo.setImgUrl(SmsApi.YU_MING+"getPhoto?photoId=/"+fileName+".jpg");    //输入的图片
            personResultMongo.setResult(persion_id); //结果
            personResultMongo.setCreateTime(new Date());  //创建日期
            personResultMongo.setOpenid(openid);  //用户openid
            personResultMongo.setType("身份证识别");
            personResultDao.save(personResultMongo);
        }else if("1".equals(ocr)){
            JSONParser parser=new JSONParser();
            JSONObject data = (JSONObject) parser.parse(result);
            MingPian mingPian = new MingPian(data);
            PersonResultMongo personResultMongo = new PersonResultMongo();
            personResultMongo.setId(KeyUtil.genUniqueKey());
            personResultMongo.setImgUrl(SmsApi.YU_MING+"getPhoto?photoId=/"+fileName+".jpg");    //输入的图片
            personResultMongo.setResult(mingPian); //结果
            personResultMongo.setCreateTime(new Date());  //创建日期
            personResultMongo.setOpenid(openid);  //用户openid
            personResultMongo.setType("名片查询");
            personResultDao.save(personResultMongo);
        }else if("2".equals(ocr)||"3".equals(ocr)||"4".equals(ocr)){
            JSONParser parser=new JSONParser();
            JSONObject data = (JSONObject) parser.parse(result);
            YingYe yingYe = new YingYe(data);
            PersonResultMongo personResultMongo = new PersonResultMongo();
            personResultMongo.setId(KeyUtil.genUniqueKey());
            personResultMongo.setImgUrl(SmsApi.YU_MING+"getPhoto?photoId=/"+fileName+".jpg");    //输入的图片
            personResultMongo.setResult(yingYe); //结果
            personResultMongo.setCreateTime(new Date());  //创建日期
            personResultMongo.setOpenid(openid);  //用户openid
            if("2".equals(ocr)){
                personResultMongo.setType("营业执照");
            }else if("3".equals(ocr)){
                personResultMongo.setType("银行卡");
            }else{
                personResultMongo.setType("通用");
            }
            personResultDao.save(personResultMongo);
        }
        return result;
    }
    @PostMapping("/getOCR_Driver")
    @ApiOperation("驾照行驶证识别")
    public String getOCr_Driver(@ApiParam("提交需要识别的照片")@RequestParam("file")MultipartFile file,
                                @ApiParam("ocr的类型参数:<br>0  ->  行驶证识别<br>1  ->驾驶证识别 ")
                                @RequestParam("type")String type,
                                @ApiParam("用户openid,可提交可不提交，不提交则不会保存") @RequestParam(value = "openid",defaultValue = "undefined") String openid)throws Exception {
        String fileName = KeyUtil.genUniqueKey();
        FileSaveLocalUtil.savePic(file.getInputStream(), fileName);
        String result = tencent_ocrService.getOCR_DriverLicense(file.getBytes(),type);
        //对信息的保存处理
        JSONParser parser=new JSONParser();
        JSONObject data = (JSONObject) parser.parse(result);
        YingYe mingPian = new YingYe(data);
        PersonResultMongo personResultMongo = new PersonResultMongo();
        personResultMongo.setId(KeyUtil.genUniqueKey());
        personResultMongo.setImgUrl(SmsApi.YU_MING+"getPhoto?photoId=/"+fileName+".jpg");    //输入的图片
        personResultMongo.setResult(mingPian); //结果
        personResultMongo.setCreateTime(new Date());  //创建日期
        personResultMongo.setOpenid(openid);  //用户openid
        if("0".equals(type)){
            personResultMongo.setType("行驶证识别");
        }else if("1".equals(type)){
            personResultMongo.setType("驾驶证识别");
        }
        personResultDao.save(personResultMongo);
        return result;
    }
}
